#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
# One login to get started, then aske for each college.
# The datasources are fished out of the returned data. 
# The periods are set in the URL.
#
# will run for previous month and all colleges unless start date (yyyy-mm-dd), end date and collegeID passed as arguments
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Add_Delta_YM Today);

use avsCommon;


my ($content, $response, $request, $url);
my $viewstate;

dbsetup();

# create web user agent and cookie jar
my $ua =  new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

getLogin();

# go through all colleges.
do {
	getStats ();
} while ($sthlogin->fetch);

dbshutdown();

exit;

sub getLogin {

	# get request sets cookies and returns viewstate
	$url = 'http://library.cqpress.com/securecenter/default.aspx';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET login $url : " . $response->status_line;
	};

	$content=$response->decoded_content;

	$content =~ /VIEWSTATE\" value\=\"(?<viewstate>.*?)\"/sm;

	# login using post and the viewstate acquired above
	$response = $ua->request ( POST $url,
		[	'__VIEWSTATE'	=> $+{viewstate},
			'txtUsername'	=> $vendorusername,
			'txtPassword'	=> $vendorpassword,
			'btnLogin'		=> '+Login+'] );

	if (!$response->is_success) {
		die "#error:$0: POST login $url" . $response->status_line;
	};

	# get report page viewstate
	$url = 'http://library.cqpress.com/securecenter/report_4.aspx';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET report page $url : " . $response->status_line;
	};

	$content=$response->decoded_content;

	$content =~ /VIEWSTATE\" value\=\"(?<viewstate>.*?)\"/sm;
	$viewstate = $+{viewstate};
};

sub getStats {
	my ($runyear, $runmonth, $runday) = @begdate;

	# run once for each month in optional range. typically beg and end month the same.
	while ($runyear < $endyear || $runyear == $endyear && $runmonth <= $endmonth) {

		my $period = myperiod($runyear, $runmonth);

		$url = 'http://library.cqpress.com/securecenter/report_4.aspx';
		$request = HTTP::Request->new(POST => $url);
		$request->content_type('application/x-www-form-urlencoded');
		$request->content('__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=' . urlencode($viewstate) .
				'&ddlTimePeriod=monthly&txtRunDate=' . urlencode($runmonth.'/1/'.$runyear) .
				'&txtHidden=&ddlUsername=' . urlencode($username) .
				'&ddlProduct=0&ddlReportType=summary&ddlTrendParameter=searches&btnRefresh=+Refresh+');

		$response = $ua->request($request);
		if (!$response->is_success) {
			die "#error:$0: POST login $url" . $response->status_line;
		};

		$content=$response->decoded_content;

		# save contents for possible debugging
		savecontent('CQPress', "$collegeid-$runyear-$runmonth", $content);

		if ($content =~ /dgdReport.*?<\/tr>(?<data>.*?<\/tr>)<tr class=\"TableFooter\">/s) {	# sometimes "No Data Returned"
			my $data = $+{data};
			$data =~ s/\s\s//g; # remove spaces/cr lf
			$data =~ s/<\/tr>/\n/g; # add cr
			$data =~ s/<\/td>/~/g; # add splitable character
			$data =~ s/<.*?>//g; # remove html
			$data =~ s/,//g; # remove commas in numbers

			while ($data =~ /^(?<datasource>.*?)~(?<searches>\d*)~/mg) {
				my $datasource = $+{datasource};
				my $searches = "$+{searches}";
				print "$vendorid $collegeid $period $datasource 0 $searches\n";
				$sthstats->execute($vendorid, $collegeid, $datasource, $period, $searches, 2)
					or die "#error: sthstats execute failed, check end of log";
			}
		}
		($runyear, $runmonth, $runday) = Add_Delta_YM($runyear, $runmonth, $runday, 0, 1);	# advance the month
	};
}